package jzbank.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Slf4j
public class OConnManagerment {


    private static ComboPooledDataSource ocpds;

    public OConnManagerment(String driver, String jdbcUrl, String user, String password) {
        OConnManagerment.ocpds = new ComboPooledDataSource();
        try {
            ocpds.setDriverClass(driver);
            ocpds.setJdbcUrl(jdbcUrl);
            ocpds.setUser(user);
            ocpds.setPassword(password);
            ocpds.setMaxStatements(550);
            ocpds.setCheckoutTimeout(15000);
            ocpds.setMaxPoolSize(Integer.valueOf(100));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static Connection getDB2connction() throws SQLException {
        Connection conn = OConnManagerment.ocpds.getConnection();
        // conn.setAutoCommit(false);
        return conn;
    }

    public static void close(ResultSet rs, PreparedStatement ps, Connection con) {
        if (rs != null){
            try {
                rs.close();
            } catch (Exception e) {
            }
        }
        if (ps != null){
            try {
                ps.close();
            } catch (Exception e) {
            }
        }
        if (con != null){
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }

    public static void closeCoon(Connection con) {
        if (con != null){
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }

    public static void closePS(ResultSet rs, PreparedStatement ps) {
        if (rs != null){

            try {
                rs.close();
            } catch (Exception e) {
            }
        }
        if (ps != null){
            try {
                ps.close();
            } catch (Exception e) {
            }
        }
    }
}
